﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class DangKi : System.Web.UI.Page
{
    DataService DB = new DataService();
    string chuoiketnoi = ConfigurationManager.ConnectionStrings["QLBH"].ConnectionString;
    SqlConnection ketnoi;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        lblTonTai.Text = "";
        Label22.Text = "";
        lbl_HoTen.Text = "";
        lbl_DiaChi.Text = "";
        lbl_SDT.Text="";
        lbl_Nick.Text = "";
        if (DB.KTTonTai(ttbTenDN.Text) == true)
        {
            lblTonTai.Text = "Tên đăng nhập đã tồn tại.";
            return;
        }
        if (ttbMatKhau1.Text != ttbMatKhau2.Text)
        {
            Label22.Text = "Xác nhận mật khẩu không đúng";
            return;
        }
        if (ttbHoTen.Text == "")
        {
            lbl_HoTen.Text = "(*)";
            return;
        }
        if (ttb_Sdt.Text == "")
        {
            lbl_SDT.Text = "(*)";
            return;
        }
        if (ttb_DiaChi.Text == "")
        {
            lbl_DiaChi.Text = "(*)";
            return;
        }
        if (ttb_Nick.Text == "")
        {
            lbl_Nick.Text = "(*)";
        }
        string Ngayf = drl_Ngay.SelectedItem.ToString();
        string Thang = drl_Thang.SelectedItem.ToString();
        string Nam = drl_Nam.SelectedItem.ToString();
        string NgaySinh = Ngayf + "/" + Thang + "/" + Nam;
        DateTime date = DateTime.Parse(NgaySinh);
        try
        {
            ketnoi = new SqlConnection(chuoiketnoi);
            ketnoi.Open();
            string sql = "insert into NguoiDung(MaQuyen,TenDangNhap,MatKhau,HoTen,Email,NgaySinh,SoDT,DiaChi,NickYahoo) values(3,@TenDangNhap,@MatKhau,@HoTen,@Email,@NgaySinh,@SoDT,@DiaChi,@NickYahoo)";
            SqlCommand cmd = new SqlCommand(sql, ketnoi);
            cmd.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar).Value = ttbTenDN.Text;
            cmd.Parameters.Add("@MatKhau", SqlDbType.NVarChar).Value = ttbMatKhau1.Text;
            cmd.Parameters.Add("@HoTen", SqlDbType.NVarChar).Value = ttbHoTen.Text;
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = ttbEmail.Text;
            cmd.Parameters.Add("@NgaySinh", SqlDbType.DateTime).Value = NgaySinh.ToString();
            cmd.Parameters.Add("@SoDT", SqlDbType.Int).Value = ttb_Sdt.Text;
            cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar).Value = ttb_DiaChi.Text;
            cmd.Parameters.Add("@NickYahoo", SqlDbType.NVarChar).Value = ttb_Nick.Text;
            cmd.ExecuteNonQuery();
            DB.DongKetNoi();
            Response.Redirect("~/DangKiThanhCong.aspx");
        }
        catch
        {
            lblThongBao.Text = "Đăng kí thất bại - xin hãy thử lại";
        }
    }
}